function [ flag ] = isIllOn( this, obj )
% DataPartition::isIllOn( this:DataPartition, 
%                         obj:meta{double, DataMatrix, DataSet} )
%              >>       [ flag:bool ]
% 
% Description
% Check whether this data partition is ill on $obj$
% I.e., whether there are missing labels in one fold

%% Check input

if nargin == 1
    error('BatErr: Invalid input arguments.');
end

%% Check illness

flag = false; % Pre-set as non-ill

Y = this.getYMatInObj(obj);

[nRep, nFld, nSubFld] = this.getSize();

% If any illness is found, then set flag into false and return
for iRep = 1:nRep
    for jFld = 1:nFld % Check for each fold
        seenId_ij = this.getZSeenId(iRep, jFld);
        if ~isIndexCoverAllLabel(Y, seenId_ij)
%             keyboard
            flag = true;
            return
        end
        
        unsnId_ij = this.getZUnsnId(iRep, jFld);
        if ~isIndexCoverAllLabel(Y, unsnId_ij)
%             keyboard
            flag = true;
            return
        end
        
        for kSubFld = 1:nSubFld % Check for each sub fold
            trnId_ijk = this.getZTrnId(iRep, jFld, kSubFld);
            if ~isIndexCoverAllLabel(Y, trnId_ijk)
%                 keyboard
                flag = true;
                return
            end
            
            valId_ijk = this.getZValId(iRep, jFld, kSubFld);
            if ~isIndexCoverAllLabel(Y, valId_ijk)
%                 keyboard
                flag = true;
                return
            end
            
            tstId_ijk = this.getZTstId(iRep, jFld, kSubFld);
            if ~isIndexCoverAllLabel(Y, tstId_ijk)
%                 keyboard
                flag = true;
                return
            end
        end
    end
end

end

function [ flag ] = isIndexCoverAllLabel( Y, id )
% DataPartition;;isIndexCoverAllLabel( Y:double, 
%                                      id:logical )
%              >>                    [ flag:bool ]
% 
% Description
% Chech whether the index in each fold or sub fold referring to subset of 
% the whole sample that (the subset) holds all labels in

Y = Y(id, :);

Y(Y == -1) = 0;

flag = all(sum(Y, 1) > 0);

end
